<?php
/**
 * Clase Base FN para BaseImTransferDetFN
 *
 * @package    
 * @subpackage BaseImTransferDetFN
 * @author     hans
 * @version    SVN:: BaseImTransferDetFN.class.php,v 1.1 2012-06-08 23:44:22 Hans.parra Exp $
 */
require_once('class/im/transfer/BN/ImTransferDetBN.class.php');
class BaseImTransferDetFN extends ImTransferDetBN
{
  private static $fieldNames = array (
    'TransferId'=>'imt_id', 'TransferDetId'=>'imtd_id', 'ArticuloId'=>'imtd_articuloId', 'Cant'=>'imtd_cant', 'Estado'=>'imtd_estado', 'CreaUsr'=>'imtd_creaUsr', 'CreaFecha'=>'imtd_creaFecha', 'ModiUsr'=>'imtd_modiUsr', 'ModiFecha'=>'imtd_modiFecha'  
  );

  public function select()
  {
    try
    {
      /*$tamPag = !property_exists($this, 'tamPag')?9999:$this->tamPag;
      $pagina = !property_exists($this, 'page')||$this->page==""?1:$this->page;
      $numeroRegistros = $this->selectNumRows();
      $limitInf = ($pagina-1)*$tamPag;*/

      $where = $this->buildConsult();
      //$orden = $this->buildOrderByField();

      $o_conexion = new BaseDatos();
      $con = $o_conexion->conectar();
      //$sql = "SELECT imt_id, imtd_id, imtd_articuloId, imtd_cant, imtd_estado, imtd_creaUsr, imtd_creaFecha, imtd_modiUsr, imtd_modiFecha FROM im_transfer_det WHERE 1 ".$where." ".$orden."  LIMIT ".$limitInf.",".$tamPag;
      $sql = "SELECT imt_id, imtd_id, imtd_articuloId, imtd_cant, imtd_estado, imtd_creaUsr, imtd_creaFecha, imtd_modiUsr, imtd_modiFecha FROM im_transfer_det WHERE 1 ".$where;
     // echo($sql);
        $result = mysql_query($sql);
      $num_rows = mysql_num_rows($result);
      $a_tmp = array();
      if($num_rows>0){
        while ($o_valor = mysql_fetch_object($result)) {
          $a_tmp[] = $this->insertObjectBN($o_valor);
        }
      }
      
      return count($a_tmp)>0?$a_tmp:null;
    }catch ( Exception $o_Error ){
      throw new Exception($o_Error->getMessage().' en '.$o_Error->getFile().' linea '.$o_Error->getLine());
    }
  }

  public function selectNumRows()
  {
    try
    {
      $where = $this->buildConsult();

      $o_conexion = new BaseDatos();
      $con = $o_conexion->conectar();
      $sql = "SELECT imt_id, imtd_id, imtd_articuloId, imtd_cant, imtd_estado, imtd_creaUsr, imtd_creaFecha, imtd_modiUsr, imtd_modiFecha FROM im_transfer_det WHERE 1 ".$where;
        $result = mysql_query($sql);
      $num_rows = mysql_num_rows($result);
      
      return $num_rows;
    }catch ( Exception $o_Error ){
      throw new Exception($o_Error->getMessage().' en '.$o_Error->getFile().' linea '.$o_Error->getLine());
    }
  }

  public function buildConsult()
  {
    try
    {
      $where = "";

      if($this->getTransferId()){ $where .= " AND imt_id = '".$this->getTransferId()."'"; }
      if($this->getTransferDetId()){ $where .= " AND imtd_id = '".$this->getTransferDetId()."'"; }
      if($this->getArticuloId()){ $where .= " AND imtd_articuloid = '".$this->getArticuloId()."'"; }
      if($this->getCant()){ $where .= " AND imtd_cant = '".$this->getCant()."'"; }
      if($this->getEstado()){ $where .= " AND imtd_estado = '".$this->getEstado()."'"; }
      if($this->getCreaUsr()){ $where .= " AND imtd_creausr = '".$this->getCreaUsr()."'"; }
      if($this->getCreaFecha()){ $where .= " AND imtd_creafecha = '".$this->getCreaFecha()."'"; }
      if($this->getModiUsr()){ $where .= " AND imtd_modiusr = '".$this->getModiUsr()."'"; }
      if($this->getModiFecha()){ $where .= " AND imtd_modifecha = '".$this->getModiFecha()."'"; }
      return $where;
    }catch ( Exception $o_Error ){
      throw new Exception($o_Error->getMessage().' en '.$o_Error->getFile().' linea '.$o_Error->getLine());
    }
  }

  public function buildOrderByField()
  {
    try
    {
      $order_by = "";

      if(property_exists($this,'sortCampos')){
      if($this->sortCampos != ""){
        $a_valCampos = explode(";", $this->sortCampos);
        $a_valtype   = explode(";", $this->sortType);
        $order_by = " ORDER BY ";
        $a_order = array();
        foreach ($a_valCampos as $key => $val){
          $sTmpOrder = "";
          if(array_key_exists($val, self::$fieldNames)){
            $sTmpOrder .= self::$fieldNames[$val];
            switch ($a_valtype[$key]){
              case 1: $sTmpOrder .= " ASC"; break;
              case 2: $sTmpOrder .= " DESC"; break;
            }
            $a_order[] = $sTmpOrder;
          }
        }
        $order_by .= implode(", ", $a_order);
      }
      }
      return $order_by;
    }catch ( Exception $o_Error ){
      throw new Exception($o_Error->getMessage().' en '.$o_Error->getFile().' linea '.$o_Error->getLine());
    }
  }

  /* B�squeda del registro por la llave primaria */
  public function selectByPK()
  {
    try
    {
      $o_conexion = new BaseDatos();
      $con = $o_conexion->conectar();
      $sql = "SELECT imt_id, imtd_id, imtd_articuloId, imtd_cant, imtd_estado, imtd_creaUsr, imtd_creaFecha, imtd_modiUsr, imtd_modiFecha FROM im_transfer_det WHERE 1 AND imt_id='".$this->getTransferId()."' AND imtd_id='".$this->getTransferDetId()."'; ";
        $result = mysql_query($sql);
      $o_Propel = $result ? mysql_fetch_object($result) : null;
      

      return $o_Propel?$this->insertObjectBN($o_Propel):null;

    }catch ( Exception $o_Error ){
      throw new Exception($o_Error->getMessage().' en '.$o_Error->getFile().' linea '.$o_Error->getLine());
    }
  }

  /* Devuelve un objeto con m�todos de acceso */
  private function insertObjectBN($o_propel){
    $o_BaseImTransferDetFN = new ImTransferDetBN();
    $o_BaseImTransferDetFN->setTransferId($o_propel->imt_id);
    $o_BaseImTransferDetFN->setTransferDetId($o_propel->imtd_id);
    $o_BaseImTransferDetFN->setArticuloId($o_propel->imtd_articuloId);
    $o_BaseImTransferDetFN->setCant($o_propel->imtd_cant);
    $o_BaseImTransferDetFN->setEstado($o_propel->imtd_estado);
    $o_BaseImTransferDetFN->setCreaUsr($o_propel->imtd_creaUsr);
    $o_BaseImTransferDetFN->setCreaFecha($o_propel->imtd_creaFecha);
    $o_BaseImTransferDetFN->setModiUsr($o_propel->imtd_modiUsr);
    $o_BaseImTransferDetFN->setModiFecha($o_propel->imtd_modiFecha);
    return $o_BaseImTransferDetFN;
  }
}